package com.igeek.boot.mapper;

import com.igeek.boot.entity.Orders;
import com.igeek.boot.entity.UserCustom;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserAndOrderMapper {
    //1.根据订单号查询 用户姓名
    @Select("select u.name from user u join orders o on u.id=o.user_id where o.id=#{oid}")
    public String selectNameByUserAndOrder(int oid);

    //2.根据用户id查询 返回用户信息 以及 所属用户的订单有哪些?
    @Select("select * from user where id=#{uid}")
    @Results({
            @Result(id = true,column = "id",property = "id"), //主键字段的映射
            @Result(
                    column = "id",
                    property = "ordersList",
                    javaType = java.util.List.class,
                    many = @Many(select = "com.igeek.boot.mapper.UserAndOrderMapper.selectOrdersByUid")),
                    //非主键字段的映射 一对一@One / 一对多@Many
            }
    )
    public UserCustom selectListByUserAndOrder(int uid);

    @Select("select * from orders where user_id=#{uid}")
    public List<Orders> selectOrdersByUid(int uid);

}
